package com.sitech.ibnms.util.charts.fusion.angulargauge;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.sitech.ibnms.util.charts.domain.HIS_DATA_DOMAIN;
import com.sitech.ibnms.util.charts.fusion.FusionChartCommon;

public class FusionAGChart {
	public static void main(String[] args) {
		FusionAGChart fs = new FusionAGChart();
		FusionAGChartParams parameter = new FusionAGChartParams();
		HIS_DATA_DOMAIN hisData = new HIS_DATA_DOMAIN();
		hisData.setKPI_VALUE("20");
		String html = fs.getAngularGaugeChart(parameter, null);

	}

	public String getAngularGaugeChart(FusionAGChartParams parameter,
			HIS_DATA_DOMAIN hisData) {
		String xmlDate = setAngularGaugeParameter(parameter, hisData);
		return FusionChartCommon.getScript(parameter).replaceAll("######",
				xmlDate);
	}

	private String setAngularGaugeParameter(FusionAGChartParams parameter,
			HIS_DATA_DOMAIN hisData) {

		Document document = DocumentHelper.createDocument();
		try {
			Element chartHead = document.addElement("chart");
			String headAttribute[] = FusionAGChartParamsName.paramName;
			String headAttributeValue[] = FusionAGChartParams.getParamVaule(
					parameter, headAttribute);
			int idx = 0;
			for (String name : headAttribute) {
				chartHead.addAttribute(name, headAttributeValue[idx]);
				idx++;
			}
			setChildParam(chartHead, "dials", hisData);
			setColorRange(chartHead, "colorRange");
			setAnnotations(chartHead, parameter);
		} catch (Exception ex) {
			System.out
					.println("add root chart has exception ,method : setLineChartParameter();");
			ex.printStackTrace();
		}
		return formatXMLData(document);

	}

	private void setAnnotations(Element chart, FusionAGChartParams parameter) {
		String url1 = "";
		String urlName1 = "";
		if (parameter != null) {
			url1 = parameter.getUrl1();
			urlName1 = parameter.getUrlName1();
			url1 = url1 != null ? url1 : "";
			urlName1 = urlName1 != null ? urlName1 : "";
		}
		if (!"".equals(url1)) {
			Element annotations = chart.addElement("annotations");
			Element annotationGroup = annotations.addElement("annotationGroup");
			annotationGroup.addAttribute("id", "1");
			annotationGroup.addAttribute("xPos", "50");
			annotationGroup.addAttribute("yPos", "30");
			annotationGroup.addAttribute("showBelow", "1");
			annotationGroup.addAttribute("link", "n-" + url1);
			Element annotation = annotationGroup.addElement("annotation");
			annotation.addAttribute("xPos", "0");
			annotation.addAttribute("yPos", "0");
			annotation.addAttribute("type", "text");
			annotation.addAttribute("label", urlName1);
			annotation.addAttribute("fontColor", "0000ff");
			annotation.addAttribute("fontSize", "14");
			annotation.addAttribute("isBold", "1");
		}
	}

	private void setChildParam(Element parent, String childName,
			HIS_DATA_DOMAIN childData) {
		String value = "0";
		if (childData != null) {
			value = childData.getKPI_VALUE();
			value = value != null ? value : "0";
		}
		try {

			Element chartNode = parent.addElement(childName);
			Element chartNode1 = chartNode.addElement("dial");
			chartNode1.addAttribute("id", "CS");
			chartNode1.addAttribute("value", value);
			chartNode1.addAttribute("rearExtension", "10");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void setColorRange(Element parent, String childName) {
		try {
			Element chartNode = parent.addElement(childName);
			Element chartNode1 = chartNode.addElement("color");
			chartNode1.addAttribute("minValue", "0");
			chartNode1.addAttribute("maxValue", "75");
			chartNode1.addAttribute("code", "8BBAF0");
			Element chartNode2 = chartNode.addElement("color");
			chartNode2.addAttribute("minValue", "75");
			chartNode2.addAttribute("maxValue", "90");
			chartNode2.addAttribute("code", "F6BD0F");
			Element chartNode3 = chartNode.addElement("color");
			chartNode3.addAttribute("minValue", "90");
			chartNode3.addAttribute("maxValue", "100");
			chartNode3.addAttribute("code", "FF654F");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void setChartStyle(Element parent, String childName) {

	}

	private String formatXMLData(Document document) {
		String xml[] = document.asXML().split("\n");
		return xml[1].replace("\"", "\'");
	}
}
